package com.ideaction.entity.version;

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.ArrayList;
import java.util.List;

@Document
@Getter
@Setter
public class ClientVersion implements Comparable<ClientVersion> {
    
    @Id
    private String id;
    
    private String channel;// 渠道
    
    private String sourceVersion;// 指定版本值
    
    private String version;// 更新到的版本
    
    private String versionUrl;// 版本资源路劲
    
    private boolean specialUp;// 指定版本更新标识
    
    private byte upType;// 1强制更新,2热更新
    
    private boolean forceUp;// 是否必须更新才能进入游戏
    
    private List<String> whiteIpList = new ArrayList<>();
    
    public boolean canPareVersion(String channel, String version, String ip) {
        if (!this.channel.equals(channel)) {
            return false;
        }
        return whiteIpList.isEmpty() ? true
                : whiteIpList.contains(ip) && getVersionValue(this.version) > getVersionValue(version);
    }
    
    private int getVersionValue(String version) {
        int toatal = 0;
        String[] vList = version.replace(".", "").split("");
        for (int i = 0; i < vList.length; i++) {
            toatal += Integer.parseInt(vList[i]) * (Math.pow(10, vList.length - 1 - i));
        }
        return toatal;
    }
    
    @Override
    public int compareTo(ClientVersion clientVersion) {
        return getVersionValue(clientVersion.getVersion()) - getVersionValue(this.version);
    }
}
